package org.taobao.ibatiside.ui.adapter;

import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.ui.model.WorkbenchAdapter;
import org.taobao.ibatiside.core.filemodel.FileProber;
import org.taobao.ibatiside.core.filemodel.IbatisMapperFile;
import org.taobao.ibatiside.core.filemodel.IbatisStatement;
import org.taobao.ibatiside.ui.Activator;

public class MapFileAdapter extends WorkbenchAdapter {
	private final IbatisMapperFile file;
	public static final RGB DISABLE_RGB = new RGB(255, 0, 0);

	public MapFileAdapter(IbatisMapperFile file) {
		super();
		this.file = file;
	}

	@Override
	public Object getParent(Object object) {
		return file.getParent();
	}

	@Override
	public String getLabel(Object object) {
		return file.getResourceName();
	}

	@Override
	public RGB getBackground(Object element) {
		return file.getSqlmapFile() == null ? DISABLE_RGB : super
				.getBackground(element);
	}

	@Override
	public ImageDescriptor getImageDescriptor(Object object) {
		return Activator.getIcon("obj16/ibatismap.png");
	}

	@Override
	public Object[] getChildren(Object object) {
		if (file.getSqlmapFile() != null && file.getSqlmapFile().isAccessible()) {
			List<IbatisStatement> stmts = FileProber.getMapStatements(file
					.getSqlmapFile());
			for (int i = 0; i < stmts.size(); i++) {
				stmts.get(i).setMapperFile(file);
			}
			return stmts.toArray();
		}
		return NO_CHILDREN;
	}
}
